# 种群类
import J_Fast_Sort

class Population:

    def __init__(self):

        self.Pop_Size = 100  # 种群规模
        self.Population_Member = []  # 种群的个体集
        self.Nodominate_Ship = []  # 支配关系 5个个体的Pareto层级：[[1], [3], [4], [2], [0]]，存储下标（注意内部无序）
        self.Unduplicated_Pareto_Num = 0  # Pareto中非重复个体的数量
        self.Unduplicated_Pareto = []  # Pareto中非重复个体的集合
        self.Unduplicated_Pareto_Obj = []  # Pareto中非重复个体的目标值
        # ========= 扩展使用 =========#
        self.Unduplicated_Pop_Num = 0  # 种群中非重复个体的数量
        self.Unduplicated_Pop = []  # 种群中非重复个体的集合
        self.Crowding_Distance = []  # 种群个体的拥挤度

    # 更新种群 根据外部个体集合更新当前种群
    def Update_Pop(self, Pop_Member):
        self.Population_Member = []  # 置空
        for i in range(self.Pop_Size):
            self.Population_Member.append(Pop_Member[i])

    # 计算种群支配关系 和 Pareto前沿面上各种信息
    # 注意：每次种群更新  更新支配关系 以防重复计算
    def Determine_Nodominate_Ship(self):
        self.Nodominate_Ship = J_Fast_Sort.Fast_Nodominate_Sort(self.Population_Member)
        # self.Determine_Pareto_Info()  # 计算Pareto信息

    # Pareto前沿面信息计算
    def Determine_Pareto_Info(self):
        Temp_Pub = []
        Pareto = self.Nodominate_Ship[0]
        Unduplicated_Pareto = []  # 未重复的Pareto
        for i in range(len(Pareto)):
            Temp = []
            Temp.append(self.Population_Member[Pareto[i]].Job_Chromosome.tolist())
            Temp.append(self.Population_Member[Pareto[i]].Machine_Chromosome.tolist())
            if Temp not in Temp_Pub:
                Temp_Pub.append(Temp)
                Unduplicated_Pareto.append(Pareto[i])
        # 目标值
        Unduplicated_Pareto_Obj = []
        for i in Unduplicated_Pareto:
            Unduplicated_Pareto_Obj.append(self.Population_Member[i].Adaptability)

        self.Unduplicated_Pareto = Unduplicated_Pareto
        self.Unduplicated_Pareto_Num = len(Unduplicated_Pareto)
        self.Unduplicated_Pareto_Obj = Unduplicated_Pareto_Obj







